package com.hitqz.robot.biz.flow.component.agv;

import com.alibaba.fastjson.JSONObject;
import com.hitqz.robot.api.business.enums.RobotOtherCommandEnum;
import com.hitqz.robot.api.business.model.dto.FlowContextDto;
import com.hitqz.robot.api.business.model.dto.FlowPatrolPreAndEndTicketsDto;
import com.hitqz.robot.biz.flow.util.LiteFlowUtil;
import com.hitqz.robot.common.core.util.R;
import com.hitqz.robot.driver.parent.RobotDriverService;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

/**
 * @author xupkun
 * @date 2024/6/15
 */
@Slf4j
@LiteflowComponent(value = "LiftUpJackStatus", name = "[轮对/轮轴]顶升-上升")
@AllArgsConstructor
public class LiftUpJackStatus extends NodeComponent {
    private final RobotDriverService robotDriverService;


    @Override
    public void process() throws Exception {
        LiteFlowUtil.checkInterruptFlag();
        FlowContextDto dto =  this.getRequestData();
        FlowPatrolPreAndEndTicketsDto ticketsDto = JSONObject.parseObject(dto.getTicketsTask()
                .getParseOrderParams(), FlowPatrolPreAndEndTicketsDto.class);
        String auto = ticketsDto.getModalData().get("auto")== null ? "0":
                String.valueOf(ticketsDto.getModalData().get("auto"));
        if (auto.equals("0")) {
            log.info("半自动任务:");
            return ;
        }
        robotDriverService.executeOtherCommand(RobotOtherCommandEnum.JACKING_UP,"shelf/s0002.shelf");
        TimeUnit.SECONDS.sleep(8);
        R result = robotDriverService.executeOtherCommand(RobotOtherCommandEnum.JACKING_STATUS,null);
        int  jackStatus = (int) result.getData();
        int time = 0;
        while (1 != jackStatus) {
            result = robotDriverService.executeOtherCommand(RobotOtherCommandEnum.JACKING_STATUS,null);
            jackStatus = (int) result.getData();
            //if (time > 50) {
            //    robotDriverService.executeOtherCommand(RobotOtherCommandEnum.JACKING_UP,null);
            //    time = 0;
            //}
            //log.info("jackStatus" + jackStatus);
            LiteFlowUtil.checkInterruptFlag();
            time++;
        }
        log.info("lifter up finished");
    }
}
